﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Custom;
using PlugNT.Database;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.BLL
{
    public class Ad
    {

        private static readonly PlugNT.Cms.DAL.Ad dal = new PlugNT.Cms.DAL.Ad();

        /// <summary>
        /// 创建一个页面的链接(由多个链接对象组成)
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public string CreateAdLinks(int classId)
        {
            return dal.CreateAdLinks(classId);
        }

        /// <summary>
        /// 删除所有的链接
        /// </summary>
        /// <returns></returns>
        public void RemoveAdLinks()
        {
            dal.RemoveAdLinks();
        }

        /// <summary>
        /// 删除一个页面的链接
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public bool RemoveAdLinks(int classId)
        {
            return PlugNT.Cms.DAL.Ad.RemoveAdLinks(classId);
        }

        /// <summary>
        /// 得到模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AdInfo GetAdInfo(int id)
        {
            return dal.GetAdInfo(id);

        }

        


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(AdInfo model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(AdInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 删除多个
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool DeleteByClassId(int classId)
        {
            return dal.DeleteByClassId(classId);
        }


        public DataTable GetList(int topCount, int currPageCount, int classId, out int recordCount)
        {
            StringBuilder andWhere = new StringBuilder();
            //标题
            if (classId>0)
            {
                andWhere.Append(" and [class_id]=" + classId);
            }
            recordCount = dal.GetCount(andWhere.ToString());
            LeftJoinInfo joininfo=new LeftJoinInfo(AdClass.GetTableName(),"name","class_id","id","");
            return dal.GetList(topCount, currPageCount, andWhere.ToString(), joininfo);
        }



        /// <summary>
        /// 返回代码
        /// </summary>
        /// <param name="ainfo"></param>
        /// <returns></returns>
        public string GetAdCoder(AdInfo ainfo)
        {
            //0:href      1:title     2:movie/src     3:width      4:height      5:wmode   

            //禁用返回空
            if (ainfo.disabled)
            {
                return "";
            }

            //全局时间(在Ad.ashx中调用)
            string ainfo_nid = ainfo.id == 0 ? ainfo.no : ainfo.id.ToString();
            string timeGlobal = "plugad_gtime";
            string startTime = "plugad_stime_" + ainfo_nid;
            string endTime = "plugad_etime_" + ainfo_nid;
            string idCode = "plugad_" + ainfo_nid;

            //返回JS脚本
            StringBuilder sbTmp = new StringBuilder();
            sbTmp.Append("\n\n");
            sbTmp.Append("var plugad_" + ainfo_nid + "='';\n");
            sbTmp.Append("var plugad_stime_" + ainfo_nid + "=new Date(" + ainfo.start_time.ToString("yyyy,MM,dd,HH,mm,ss") + ");\n");
            sbTmp.Append("var plugad_etime_" + ainfo_nid + "=new Date(" + ainfo.end_time.ToString("yyyy,MM,dd,HH,mm,ss") + ");\n");


            if (ainfo.target_url != "")
            {
                sbTmp.Append(idCode + "+='<a href=\"{0}\" title=\"{1}\" target=\"_blank\">';\n");
                if (ainfo.type == 1)
                {
                    sbTmp.Append(idCode + "+='<button disabled=\"disabled\" style=\"width:100%; height:100%; margin:0px; padding:0px; border:0px;\">';\n");
                }
            }
            //如果是动画就显示动画代码
            if (ainfo.type == 1)
            {
                sbTmp.Append(idCode + "+='<object classid=\"clsid:D27CDB6E-AE6D-11CF-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" width=\"{3}\" height=\"{4}\">';\n");
                sbTmp.Append(idCode + "+='<param name=\"movie\" value=\"{2}\">';\n");
                sbTmp.Append(idCode + "+='<param name=\"quality\" value=\"high\">';\n");
                sbTmp.Append(idCode + "+='<param name=\"wmode\" value=\"{5}\">';\n");
                sbTmp.Append(idCode + "+='<param name=\"menu\" value=\"false\">';\n");
                sbTmp.Append(idCode + "+='<embed src=\"{2}\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"{3}\" height=\"{4}\" quality=\"High\" wmode=\"{5}\">';\n");
                sbTmp.Append(idCode + "+='</embed>';\n");
                sbTmp.Append(idCode + "+='</object>';\n");
            }
            else
            {
                sbTmp.Append(idCode + "+='<img src=\"{2}\" style=\"width:{3}; height:{4}; border:0px;\">';\n");
            }
            if (ainfo.target_url != "")
            {
                if (ainfo.type == 1)
                {
                    sbTmp.Append(idCode + "+='</button>';\n");
                }
                sbTmp.Append(idCode + "+='</a>';\n");
            }
            sbTmp.Append("if(typeof(document.getElementById('" + ainfo.target_html_id + "'))!='undefined'){\n");
            sbTmp.Append("  if(" + timeGlobal + ">" + startTime + " && " + timeGlobal + "<" + endTime + "){\n");
            sbTmp.Append("    document.getElementById('" + ainfo.target_html_id + "').innerHTML=" + idCode + ";\n");
            sbTmp.Append("  }else{\n");
            sbTmp.Append("    document.getElementById('" + ainfo.target_html_id + "').innerHTML='';\n");
            sbTmp.Append("  }\n");
            sbTmp.Append("}\n");

            string reString=sbTmp.ToString();
            reString=reString.Replace("{0}",ainfo.target_url);
            reString=reString.Replace("{1}",ainfo.description);
            reString=reString.Replace("{2}",ainfo.file_path);
            reString=reString.Replace("{3}",ainfo.width.ToString());
            reString=reString.Replace("{4}",ainfo.height.ToString());
            reString=reString.Replace("{5}",ainfo.target_url != "" ? "transparent" : "opaque");

            return reString;

        }
    }
}
